package view.combo;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import entiey.Combo;
import entiey.Menu;
import service.ComboService;
import service.MenuService;
import service.impl.ComboServiceImpl;
import service.impl.MenuServiceImpl;
import util.CastUtil;

import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class V_comboShow extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private ComboService cs=new ComboServiceImpl();
	private JTextField txt_query;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_comboShow frame = new V_comboShow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_comboShow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 883, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel label = new JLabel("编号/名称/拼音查询");
		
		txt_query = new JTextField();
		txt_query.setColumns(10);
		
		JButton button = new JButton("查 询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=txt_query.getText();
				List<Combo> list=cs.likeFind(text);
				initCombo(list);
			}
		});
		
		JButton button_1 = new JButton("添加新套餐");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_Combo combo=new V_Combo();
				combo.setVisible(true);
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(58)
							.addComponent(label, GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txt_query, GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
							.addGap(30)
							.addComponent(button)
							.addGap(51)
							.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE)
							.addGap(275))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 837, Short.MAX_VALUE)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(34)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(button)
						.addComponent(button_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(txt_query))
					.addGap(62)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 340, GroupLayout.PREFERRED_SIZE))
		);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u540D\u79F0", "\u62FC\u97F3\u7B80\u79F0", "\u4EF7\u683C", "\u662F\u5426\u4E0A\u67B6", "\u63CF\u8FF0", "\u5305\u542B\u83DC\u54C1"
			}
		));
		scrollPane.setViewportView(table);
		contentPane.setLayout(gl_contentPane);
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		JMenuItem menuItem = new JMenuItem("修改");
		menuItem.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				int index=table.getSelectedRow();
				int id=CastUtil.castInt(table.getValueAt(index, 0));
				
				V_comboUpdate cUpdate=new V_comboUpdate();
				cUpdate.setComboId(id);
				System.out.println(cs.findById(id));
			
				cUpdate.setVisible(true);
				
			}
		});
		popupMenu.add(menuItem);
		
		JMenuItem menuItem_1 = new JMenuItem("删除");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row=CastUtil.castInt(table.getSelectedRow());
				int id=CastUtil.castInt(table.getValueAt(row, 0));
				if (cs.delete(id)) {
					initCombo(cs.findAll());
					JOptionPane.showMessageDialog(V_comboShow.this,"套餐删除成功!");
				}else {
					JOptionPane.showMessageDialog(V_comboShow.this,"套餐删除失败!");
				}
			}
		});
		popupMenu.add(menuItem_1);

		
		//初始化
		List<Combo> list=cs.findAll();
		initCombo(list);
		
	}
	

/**
 * 初始化菜单表格
 */
public void initCombo(List<Combo> list) {
	DefaultTableModel dtm=(DefaultTableModel) table.getModel();
	int count=dtm.getRowCount();
	//清除上次选择的
	for (int i = 0; i < count; i++) {
		dtm.removeRow(0);
	}
	MenuService ms=new MenuServiceImpl();
	ms.selectMenu(30);
	
	for(Combo c: list) {
		dtm.addRow(new Object[] {
				c.getId(),
				c.getComboName(),
				c.getSpellCode(),
				c.getComboPrice(),
				c.getStatus(),
				c.getDescription(),
				ms.selectMenu(c.getId())
		});

	}}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
